package org.elec.mac.controller;

import org.elec.mac.entity.SOrder;
import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.service.SOrderService;
import org.elec.mac.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by Bean on 2018/4/19.
 */
@Slf4j
@Controller
public class OrderController extends BaseController {

    @Autowired
    private SOrderService sOrderService;

    @RequestMapping(value = "/order_list.do")
    private String orderListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                  @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                  @RequestParam(value = "limit", required = false) String limit,
                                  @RequestParam(value = "page", required = false) String page) throws IOException {


        if("page".equals(action)) {
            PageProperty pp = new PageProperty();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                this.setPageInfo(request.getCookies(), pp, limit, page);
                PageList<SOrder> lintPageList = sOrderService.getPageList(pp);
                map.put("success", true);
                map.put("records", lintPageList.getRecords());
                map.put("recordsTotal", lintPageList.getTotalRecords());
                map.put("recordsFiltered", lintPageList.getTotalRecords());
            }
            catch(Exception ex) {
                log.error("订单列表获取错误", ex);
                map.put("success", false);
                map.put("msg", "订单列表获取错误");
            }
            response.getWriter().write(JSONUtil.map2json(map));
            return null;
        }

        return "order/order_list";
    }
}
